﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Tools
{
    public partial class Tool
    {
        #region  转义json传递时导致错误的符号
        /// <summary>
        /// function 转义json传递时导致错误的符号
        /// Dason
        /// 2009-1-15
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        static string RepalceHtml(string Str)
        {
            return Str.Replace("\"", "'").Replace("\r\n", "<br>").Replace("\r", "<br>").Replace(@"
", "<br>");
        }

        #endregion

        #region 数据表转JSON

        /// <summary>
        /// function:数据表转JSON
        /// Author:Dason
        /// Date:2008-12-29
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string CreateJsonParameters(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder JsonString = new StringBuilder();
                JsonString.Append("{ ");
                JsonString.Append("\"Table\": ");
                JsonString.Append(DataTableToJson(dt));
                JsonString.Append("}");
                return JsonString.ToString();
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region  数据表转JSON
        /// <summary>
        /// function:数据表转JSON
        /// Author:Dason
        /// Date:2008-1-14
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTableToJson(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder JsonString = new StringBuilder();
                JsonString.Append("[");

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.AppendFormat("\"{0}\":\"{1}\",", dt.Columns[j].ColumnName, RepalceHtml(dt.Rows[i][j].ToString()));

                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.AppendFormat("\"{0}\":\"{1}\"", dt.Columns[j].ColumnName, RepalceHtml(dt.Rows[i][j].ToString()));

                        }
                    }

                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]");
                return JsonString.ToString();
            }
            else
                return "";
        }
        #endregion
    }
}
